# Makefile for uClibc
#
# Copyright (C) 2000-2008 Erik Andersen <andersen@uclibc.org>
#
# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
#

subdirs += ldso/ldso/$(TARGET_ARCH)

CFLAGS-ldso := -DNOT_IN_libc -DIS_IN_rtld $(SSP_DISABLE_FLAGS)

# This stuff will not work with -fomit-frame-pointer
CFLAGS-ldso += -fno-omit-frame-pointer

CFLAGS-ldso += -I$(top_srcdir)ldso/ldso/$(TARGET_ARCH) -I$(top_srcdir)ldso/include -I$(top_srcdir)ldso/ldso
CFLAGS-ldso += -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" -DUCLIBC_LDSO=\"$(UCLIBC_LDSO)\"

ifeq ($(DODEBUG),y)
# Not really much point in including debugging info, since gdb
# can't really debug ldso, since gdb requires help from ldso to
# debug things....
# On arm, gcc-4.3.x onwards -Os emits calls to libgcc, which calls _div0,
# which tries to call raise(). And raise comes from libc so a catch 22.
# Using -O2 instead. We could have use -fno-early-inlining with -Os too.

CFLAGS-ldso += -O2 -g
endif

CFLAGS-ldso/ldso/$(TARGET_ARCH)/ := $(CFLAGS-ldso)

CFLAGS-ldso.c := -DLDSO_ELFINTERP=\"$(TARGET_ARCH)/elfinterp.c\" $(CFLAGS-ldso)

LDFLAGS-$(UCLIBC_FORMAT_DSBT_ELF)-$(UCLIBC_LDSO_NAME).so := -Wl,--dsbt-index=1
ifneq ($(SUPPORT_LD_DEBUG),y)
LDFLAGS-$(UCLIBC_LDSO_NAME).so := $(LDFLAGS)
else
LDFLAGS-$(UCLIBC_LDSO_NAME).so := $(LDFLAGS_NOSTRIP) -Wl,-z,defs
endif
LDFLAGS-$(UCLIBC_LDSO_NAME).so += -Wl,-e,$(SYMBOL_PREFIX)_start -Wl,-z,now -Wl,-Bsymbolic \
	-Wl,--export-dynamic $(CFLAG_-Wl--sort-common) -Wl,--discard-locals \
	$(CFLAG_-Wl--discard-all) -Wl,--no-undefined

ldso_FULL_NAME := $(UCLIBC_LDSO_NAME)-$(VERSION).so

$(UCLIBC_LDSO_NAME)_DIR := $(top_srcdir)ldso/ldso
$(UCLIBC_LDSO_NAME)_OUT := $(top_builddir)ldso/ldso

$(UCLIBC_LDSO_NAME)_CSRC := $($(UCLIBC_LDSO_NAME)_DIR)/ldso.c
# prerequesites
$($(UCLIBC_LDSO_NAME)_OUT)/ldso.o $($(UCLIBC_LDSO_NAME)_OUT)/ldso.oS: \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-debug.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-startup.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-array.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-hash.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-elf.c
$(UCLIBC_LDSO_NAME)_COBJ := $(patsubst $($(UCLIBC_LDSO_NAME)_DIR)/%.c,$($(UCLIBC_LDSO_NAME)_OUT)/%.o,$($(UCLIBC_LDSO_NAME)_CSRC))

$(UCLIBC_LDSO_NAME)_SSRC := $(wildcard $($(UCLIBC_LDSO_NAME)_DIR)/$(TARGET_ARCH)/*.S)
$(UCLIBC_LDSO_NAME)_SOBJ := $(patsubst $($(UCLIBC_LDSO_NAME)_DIR)/$(TARGET_ARCH)/%.S,$($(UCLIBC_LDSO_NAME)_OUT)/$(TARGET_ARCH)/%.o,$($(UCLIBC_LDSO_NAME)_SSRC))

$(UCLIBC_LDSO_NAME)_OBJS := $($(UCLIBC_LDSO_NAME)_COBJ) $($(UCLIBC_LDSO_NAME)_SOBJ)

ldso-y := $($(UCLIBC_LDSO_NAME)_OBJS:.o=.oS)

lib-so-y += $(ldso)
objclean-y += CLEAN_ldso/ldso

ifeq ($(LDSO_PRELINK_SUPPORT),y)
# Use a specific linker script for ld.so
LDFLAGS-$(UCLIBC_LDSO_NAME).so += -T $(ldso:.$(ABI_VERSION)=).lds
endif

$(ldso): $(ldso:.$(ABI_VERSION)=)
$(ldso:.$(ABI_VERSION)=): $($(UCLIBC_LDSO_NAME)_OUT)/$(UCLIBC_LDSO_NAME)_so.a
ifeq ($(LDSO_PRELINK_SUPPORT),y)
	$(call create-lds)
endif
	$(call link.so,$(ldso_FULL_NAME),$(ABI_VERSION))

$($(UCLIBC_LDSO_NAME)_OUT)/$(UCLIBC_LDSO_NAME)_so.a: $(ldso-y)
	$(Q)$(RM) $@
	$(do_ar)

CLEAN_ldso/ldso:
	$(do_rm) $(addprefix $($(UCLIBC_LDSO_NAME)_OUT)/,$(foreach e, o os oS a,$(foreach d, *. */*.,$(d)$(e))))
